package net.akkz.dao.impl;

import java.util.ArrayList;

import net.akkz.dao.ImgViewDao;
import net.akkz.dbFactory.Session;
import net.akkz.exception.BuildClassError;
import net.akkz.exception.SQLExcuteException;
import net.akkz.model.ImgView;

public class ImgViewDaoImpl implements ImgViewDao
{

	@Override
	public void addImgView(Session session, ImgView imgView) throws SQLExcuteException
	{
		String sql = "insert into imgview(url, message, studentid, goodnum, state) values(?, ?, ?, ?, 0)";

		Object[] values = new Object[4];
		values[0] = imgView.getUrl();
		values[1] = imgView.getMessage();
		values[2] = imgView.getStudentid();
		values[3] = imgView.getGoodnum();
		
		session.excute(sql, values);
	}

	@Override
	public ArrayList<ImgView> findImgView(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview where state = 1";
		
		return session.find(sql, new Object[0], ImgView.class, start, end - start + 1);
	}

	@Override
	public ArrayList<ImgView> findImgViewOrderByGoodNum(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview where state = 1 order by goodnum";
		
		return session.find(sql, new Object[0], ImgView.class, start, end - start + 1);
	}

	@Override
	public ArrayList<ImgView> findImgViewByStudentId(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview where studentid = ? and state = 1";
		
		Object[] values = new Object[1];
		values[0] = studentId;
		
		return session.find(sql, values, ImgView.class);
	}

	@Override
	public ArrayList<ImgView> findAllImgView(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview";
		
		return session.find(sql, new Object[0], ImgView.class, start, end - start + 1);
	}

	@Override
	public ArrayList<ImgView> findAllImgViewOrderByGoodNum(Session session, int start, int end) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview order by goodnum";
		
		return session.find(sql, new Object[0], ImgView.class, start, end - start + 1);
	}

	@Override
	public ArrayList<ImgView> findAllImgViewByStudentId(Session session, int studentId) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview where studentid = ?";
		
		Object[] values = new Object[1];
		values[0] = studentId;
		
		return session.find(sql, values, ImgView.class);
	}

	
	@Override
	public ImgView getImgView(Session session, int imgViewid) throws SQLExcuteException, BuildClassError
	{
		String sql = "select * from imgview where id=?";
		
		return session.get(sql, imgViewid, ImgView.class);
	}

	@Override
	public void updateImgViewGood(Session session, ImgView imgView) throws SQLExcuteException
	{
		String sql = "update imgview set url=?,message=?,studentid=?,goodnum=? where id=?";

		Object[] values = new Object[5];
		values[0] = imgView.getUrl();
		values[1] = imgView.getMessage();
		values[2] = imgView.getStudentid();
		values[3] = imgView.getGoodnum();
		values[4] = imgView.getId();
		
		session.excute(sql, values);
	}

	@Override
	public void checkImgViewGood(Session session, ImgView imgView) throws SQLExcuteException
	{
		String sql = "update imgview set state=? where id=?";

		Object[] values = new Object[2];
		values[0] = imgView.getState();
		values[1] = imgView.getId();
		
		session.excute(sql, values);
	}

	@Override
	public void deleteImgView(Session session, int imgViewId) throws SQLExcuteException
	{
		String sql = "delete from imgview where id=?";

		Object[] values = new Object[1];
		values[0] = imgViewId;
		
		session.excute(sql, values);
	}

	
}
